package com.yb.java8.datetimeapi;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * DateTimeFormatter 示例
 */
public class DateTimeFormatDemo {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println("==========localDate==========");
        System.out.println(DateTimeFormatter.ISO_LOCAL_DATE.format(localDate));
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd").format(localDate));

        System.out.println("==========localDateTime==========");
        System.out.println(DateTimeFormatter.ISO_LOCAL_DATE.format(localDateTime));
        System.out.println(DateTimeFormatter.ISO_LOCAL_TIME.format(localDateTime));
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDateTime));

        // 时间字符串转自定义格式的LocalDateTime
        LocalDate date = LocalDate.parse("2018-03-03", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDateTime dateTime1 = LocalDateTime.parse("2017-03-03 15:30:05", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("====================");
        System.out.println(date);
        System.out.println(dateTime1);
    }
}
